<?php $this->load->view("admin/$adminCurrentTemplate/commonHead2");?>
<table width="99%" border="0" cellspacing="1" cellpadding="0"
	class="editFormContainer">
	<form id="editForm" name="editForm" method="post"
		action="<?=$this->input->server('REQUEST_URI')?>"><input type="hidden"
		name="id" id="id" value="<?php if(isset($id)) echo $id; ?>" />
	<tr>
		<td class="guid">启用状态：</td>
		<td class="formItem"><input type="radio" name="disabled"
			id="disabled-0" value="0" checked="checked" /><label for="disabled-0"
			<?php if(isset($disabled)) if($disabled==0) echo ' checked' ;?>>启用</label> <input
			type="radio" name="disabled" id="disabled-1" value="1"
			<?php if(isset($disabled)) if($disabled==1) echo ' checked' ;?> /><label for="disabled-1">禁用</label>
		</td>
	</tr>
	<tr>
		<td class="guid">所属分组：</td>
		<td class="formItem">
		<select name="groupId" id="groupId" size="1" class="single-select"><?php 
		$str = '';
		foreach ($adminGroupAll as $value){
			$str .= "<option value=\"".$value->id."\"";
			if($value->id==$groupId){
				$str .= " selected=\"selected\"";
			}
			$str .=">";
			for($i=0;$i<$value->deep;$i++){
				$str .= "&nbsp;|-";
			}
			$str .= $value->title."</option>\n";
		}
		echo $str;
		?></select></td>
	</tr>
	<tr>
		<td class="guid">用户名称：</td>
		<td class="formItem"><input type="text" name="userName" id="userName"
			class="sing-line input-name" value="<?php if(isset($userName)) echo $userName;?>" /></td>
	</tr>
	<tr>
		<td class="guid">用户密码：</td>
		<td class="formItem"><input type="password" name="userPWD" id="userPWD" class="sing-line password input-name" value="" /></td>
	</tr>
	<tr>
		<td class="guid">确认密码：</td>
		<td class="formItem"><input type="password" name="userPWD2" id="userPWD2" class="sing-line password input-name" value="" /></td>
	</tr>
	<tr>
		<td class="guid">管理权限：<input type="hidden" name="permissions" id="permissions" value="<?php if(isset($permission)) echo $permission;?>" /></td>
		<td class="formItem permission-container"> </td>
	</tr>
	</form>
</table>
<?php $this->load->view("admin/$adminCurrentTemplate/commonFoot2");?>
<script type="text/javascript">
var tvName = '<?php if(isset($tvName)) echo $tvName;?>';
$(function(){
	$('#userName').bind('blur',userNameBlur);
	var id = $('#id').val();
	if(id.length==0) dg.SetTitle('添加系统用户');
	dg.addBtn('subBtn','提 交',subForm);
	$('#groupId').bind('change',groupChange).change()
})

var userNameBlur = function(){
	var id = $.trim($('#id').val());
	var userName = $.trim($('#userName').val());
	$.ajax({
		type:'POST',
		url:getInfo + '/check/',
		data:'tvName=' + tvName + '&fieldName=userName&fieldValue=' + userName + '&id=' + id,
		success:function(returnValue){
			if(parseInt(returnValue)>0){
				subMess(310,108,'用户名错误','<span class="red">对不起，用户名已经存在,请选择其它用户名！</span>',$("#userName"));
				return false;
			}
		}
	});
}

var groupChange = function(){
	var thisValue = $(this).val();
	$.ajax({
		type:'POST',
		url:getInfo + '/../permission',
		data:'adminGroupParentId=' + thisValue,
		success:function(returnValue){
			var permissions = $('#permissions').val();
			$('.permission-container').html(returnValue);
			$('.permission-container .permission-0').each(function(){
				var thisId = $(this).children().eq(0).attr('id');
				if($('.' + thisId).length==0) $(this).remove();
			});
			$('.permission-container input[name="p_all"]').bind('click',p_all);
			$('.permission-container input[name^="permission"]').bind('click',p_click);
			if(permissions.length>0){
				permissions = getJSON(permissions);
				$('.permission-container input[name^="permission"]').each(function(){
					var thisV = $(this).val().split('_');
					var v_key = thisV[0];
					var v_val = thisV[1];
					try{
						if(permissions[v_key][v_val]){
							$(this).click();
						}
					}catch(e){}
				});
			}
		}
	});
}

var p_all = function(){
	var thisId = $(this).attr('id');
	if(this.checked){
		$('.' + thisId).children().attr('checked',true);
	}else{
		$('.' + thisId).children().removeAttr('checked');
	}
}

var p_click = function(){
	var thisValue = $(this).val();
	var parentId = $(this).parents('div').attr('class');
	var inputLength = $('.' + parentId + ' input').length;
	var checkedLength = $('.' + parentId + ' input:checked').length;
	if(inputLength==checkedLength){
		$('#' + parentId).attr('checked',true);
	}else{
		$('#' + parentId).removeAttr('checked');
	}
}

var subForm = function(){
	var id = $.trim($('#id').val());
	var disabled = $.trim($('#disabled').val());
	var userName = $.trim($('#userName').val());
	var userPWD = $.trim($('#userPWD').val());
	var userPWD2 = $.trim($('#userPWD2').val());
	if(userName.length==0){
		subMess(300,108,'用户名错误','<span class="red">对不起，用户名不能为空,请输入用户名！</span>',$("#userName"));
		return false;
	}
	if(id.length>0){
		if(userPWD!=userPWD2){
			$('#userPWD').val('');
			$('#userPWD2').val('');
			subMess(320,108,'密码错误','<span class="red">对不起，两次输入的密码不相同,请重新输入！</span>',$("#userPWD"));
			return false;
		}
	}else{
		if(userPWD.length==0){
			subMess(320,108,'密码错误','<span class="red">对不起，用户密码不能为空,请输入用户密码！</span>',$("#userPWD"));
			return false;
		}
		
		if(userPWD!=userPWD2){
			$('#userPWD').val('');
			$('#userPWD2').val('');
			subMess(320,108,'密码错误','<span class="red">对不起，两次输入的密码不相同,请重新输入！</span>',$("#userPWD"));
			return false;
		}
	}	

	var permissions = perms = '';
	$('.permission-container input[name^="permission"]:checked').each(function(i){
		var thisV = $(this).val().split('_');
		var v_key = thisV[0];
		var v_val = thisV[1];
		if(perms.length==0){
			permissions = '{' + v_key + ':{' + v_val + ':true';
		}else{
			if(perms==v_key){
				permissions += ',' + v_val + ':true';
			}else{
				permissions += '},' + v_key + ':{' + v_val + ':true';
			}
		}
		perms = v_key;
		if(i==$('.permission-container input[name^="permission"]:checked').length-1){
			permissions += '}}';
		}
	});
	$('#permissions').val(permissions);
	$('.permission-container .permission-ul').remove();
	
	$('#editForm').submit();
}
</script>
